

. .\Lib.Strings.ps1
. .\Lib.MimeType.ps1
. .\Lib.WebPage.ps1


#====================================================================
#
#		FILE BROWSER WEB APPLICATION
#
#====================================================================


#--------------------------------------------------------------------
Function Get-HtmlFolderList
{
	param
	(
		  [psobject] $WebRequest
	)
	[string] $html = ""
	$html += "<div id=""folder_list"">"
	$html += "<b>Folders</b>"
	$html += "<table>"
	[string] $path = $WebRequest.WebServer.ApplicationFolder
	[string] $url = $WebRequest.Protocol + "://" + $WebRequest.WebServer.ServerAddress
	if( $WebRequest.ResourceName -ne "" )
	{
		$path += "\" + $WebRequest.ResourceName
		$url += "/" + $WebRequest.ResourceName.Replace( "\", "/" )
	}
	[psobject] $Items = Get-ChildItem -Path $path
	[psobject] $Item = $null
	foreach( $Item in $Items )
	{
		[string] $Attrs = $Item.Attributes.ToString()
		if( $Attrs.Contains( "Directory" ) )
		{
			$html += "<tr>"
			$html += "<td>[ <a href=""$( $url + "/" + $Item.Name )"">$( $Item.Name )</a> ]</td>"
			$html += "<td>$( $Item.LastWriteTime )</td>"
			[int] $n = $Item.GetDirectories( "*" ).Count
			if( $n -eq 0 )
				{ $html += "<td>no folders</td>" }
			elseif( $n -eq 1 )
				{ $html += "<td>1 folder</td>" }
			else
				{ $html += "<td>$n folders</td>" }
			$n = $Item.GetFiles( "*" ).Count
			if( $n -eq 0 )
				{ $html += "<td>no files</td>" }
			elseif( $n -eq 1 )
				{ $html += "<td>1 files</td>" }
			else
				{ $html += "<td>$n files</td>" }
			$html += "</tr>"
		}
	}
	$html += "</table>"
	$html += "</div>"
	return $html
}


#--------------------------------------------------------------------
Function Get-HtmlFileList
{
	param
	(
		  [psobject] $WebRequest
	)
	[string] $html = ""
	$html += "<div id=""file_list"">"
	$html += "<b>Files</b>"
	$html += "<table>"
	[string] $path = $WebRequest.WebServer.ApplicationFolder
	[string] $url = $WebRequest.Protocol + "://" + $WebRequest.WebServer.ServerAddress
	if( $WebRequest.ResourceName -ne "" )
	{
		$path += "/" + $WebRequest.ResourceName
		$url += "/" + $WebRequest.ResourceName.Replace( "\", "/" )
	}
	[psobject] $Items = Get-ChildItem -Path $path
	[psobject] $Item = $null
	[string] $MimeType = ""
	foreach( $Item in $Items )
	{
		[string] $Attrs = $Item.Attributes.ToString()
		if( -not $Attrs.Contains( "Directory" ) )
		{
			$html += "<tr>"
			$html += "<td><a href=""$( $url + "/" + $Item.Name )"">$( $Item.Name )</a></td>"
			$html += "<td>$( $Item.Length )</td>"
			$html += "<td>$( $Item.LastWriteTime )</td>"
			$MimeType = Get-ItemMimeType $Item.Name
			$html += "<td>$( $MimeType )</td>"
			$html += "</tr>"
		}
	}
	$html += "</table>"
	$html += "</div>"
	return $html
}


#--------------------------------------------------------------------
Function Process-Web-GET
{
	param
	(
		  [psobject] $WebRequest
	)

	[psobject] $WebResponse = New-WebResponse
	$WebResponse.WebRequest		= $WebRequest
	$WebResponse.Resource		= $null
	$WebResponse.ResponseCode	= "501 Not Implemented"
	
	# Construct the request path.
	[string] $ResourcePath = $WebRequest.WebServer.ApplicationFolder
	[string] $ResourceName = $WebRequest.ResourceName
Write-Host "GET -> $( $ResourcePath + "\" + $ResourceName )"

	# Check if requested path is a directory.
	if( Test-Path -Path $( $ResourcePath + "\" + $ResourceName ) -PathType container )
	{
		# Build the web page.
		[string] $html = Get-HtmlHeader $WebRequest
		$html += "<h3>$( $WebRequest.WebServer.ApplicationName )</h3>"
		$html += Get-HtmlNavigationMenu $WebRequest
		$html += "<br />"
		$html += Get-HtmlFolderList $WebRequest
		$html += "<br />"
		$html += Get-HtmlFileList $WebRequest
		$html += "<br />"
		$html += Get-HtmlFooter $WebRequest
		
		# Package the html in a new resource..
		[psobject] $Resource = New-Resource
		$Resource.Root = $ResourcePath
		$Resource.Path = $ResourceName
		$Resource.Name = ""
		$Resource.Mime = "text/html"
		$Resource.Data = AsciiString2Bytes( $html )
		$Resource.Size = $Resource.Data.Length

		# Construct the response.
		$WebResponse.Resource = $Resource
		$WebResponse.ResponseCode = "200 OK"
	}
	elseif( Test-Path -Path $( $ResourcePath + "\" + $ResourceName ) -PathType leaf )
	{
		# Resource is a file
		$WebResponse.Resource = Read-Resource $ResourcePath $ResourceName
		$WebResponse.ResponseCode = "200 OK"
	}
	else
	{
		# Not found
		$WebResponse.ResponseCode = "404 Not Found"
	}
	
	# Send the response header.
Write-Host $WebResponse.ResponseCode
	[bool] $b = Send-WebResponseHeader $WebResponse
	if( $b -eq $false ) { return $false }

	# Send the resource content.
	if( $WebResponse.Resource -ne $null )
	{
		#Write-Debug -Message $Resource.Data
	    $cbSent = $Socket.Send( $WebResponse.Resource.Data )
	    if ( $cbSent -eq -1 ) { return $false }
    }
	return $true

}

